package com.jeasonzhao.commons.chart.model;

import java.util.Vector;

import com.jeasonzhao.commons.utils.Algorithms;

public class Chart extends Box
{
    private String title = null;
    private String subTitle = null;
    private TextAlign titleAlign = TextAlign.Center;
    private TextAlign subTitleAlign = TextAlign.Center;
    private java.util.Vector<Series> dataSeries = null;
//    private Axis axis = null;
    private RenderType defaultRenderType = RenderType.Unknown;
    private Vector<String> axisLabels = null;
    public Chart()
    {
    }

    public Vector<Series> getDataSeries()
    {
        return dataSeries;
    }

    public String getSubTitle()
    {
        return subTitle;
    }

    public TextAlign getSubTitleAlign()
    {
        return subTitleAlign;
    }

    public String getTitle()
    {
        return title;
    }

    public TextAlign getTitleAlign()
    {
        return titleAlign;
    }

    public Vector<String> getAxisLabels()
    {
        return axisLabels;
    }

    public void setTitleAlign(TextAlign titleAlign)
    {
        this.titleAlign = titleAlign;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public void setSubTitleAlign(TextAlign subTitleAlign)
    {
        this.subTitleAlign = subTitleAlign;
    }

    public void setSubTitle(String subTitle)
    {
        this.subTitle = subTitle;
    }

    public void setDataSeries(Vector<Series> dataSeries)
    {
        this.dataSeries = dataSeries;
    }

    public void setAxisLabels(Vector<String> axisLabels)
    {
        this.axisLabels = axisLabels;
    }

    public Chart addData(Series ...dataseries)
    {
        if(null != dataseries)
        {
            for(Series s : dataseries)
            {
                if(null != s)
                {
                    if(null == this.dataSeries)
                    {
                        this.dataSeries = new Vector<Series>();
                    }
                    this.dataSeries.add(s);
                }
            }
        }
        return this;
    }

    public int seriesCount()
    {
        return null == this.dataSeries ? 0 : this.dataSeries.size();
    }

    public boolean containsRenderType(RenderType t)
    {
        RenderType dt = getTypicalRenderType();
        if(dt == t)
        {
            return true;
        }
        else if(null != this.dataSeries)
        {
            for(Series s : this.dataSeries)
            {
                if(s.getRenderType() == t)
                {
                    return true;
                }
            }
            return false;
        }
        else
        {
            return false;
        }
    }

    public RenderType getTypicalRenderType()
    {
        if(this.defaultRenderType != null && this.defaultRenderType != RenderType.Unknown)
        {
            return this.defaultRenderType;
        }
        else if(null != this.dataSeries)
        {
            for(Series s : this.dataSeries)
            {
                if(s != null && s.getRenderType() != null && s.getRenderType() != RenderType.Unknown)
                {
                    return s.getRenderType();
                }
            }
            return RenderType.Unknown;
        }
        else
        {
            return RenderType.Unknown;
        }
    }

    public static MainAxis getCategoryAxis(RenderType t)
    {
        switch(t)
        {
            case StackedBar:
            case StackedBar100:
            case Bar:
                return MainAxis.Y;
            case Column:
            case Line:
            case Pie:
            case Area:
            case Doughnut:
            case StackedColumn:
            case StackedColumn100:
            case StackedArea:
            case StackedArea100:
            case Bubble:
            case Point:
            case Funnel:
            case Funnel100:
            case CandleStick:
            case Stock:
            case Unknown:
            default:
                return MainAxis.X; //Show values in Y-axis
        }
    }

    public Vector<String> getTypicalAxisLabels()
    {
        if(null != this.axisLabels && this.axisLabels.size() > 0)
        {
            return this.axisLabels;
        }
        this.axisLabels = new Vector<String>();
        for(Series s : this.dataSeries)
        {
            if(null == s)
            {
                continue;
            }
            for(Point pt : s.getValues())
            {
                String ct = pt.getCategory();
                if(Algorithms.notEmpty(ct) && this.axisLabels.contains(ct.trim()) == false)
                {
                    this.axisLabels.add(ct.trim());
                }
            }
        }
        return this.axisLabels;
    }

    //extract legends from data
    public Vector<String> getLegends()
    {
        if(null == this.dataSeries || this.dataSeries.size() < 1)
        {
            return null;
        }
        Vector<String> ret = new Vector<String>();
        for(Series s : this.dataSeries)
        {
            if(null != s && Algorithms.notEmpty(s.getTitle()))
            {
                ret.add(s.getTitle());
            }
        }
        return ret.size() < 1 ? null : ret;
    }
}
